/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package net.qiujuer.genius.ui.utils;

import ohos.agp.colors.RgbColor;
import ohos.agp.components.ComponentState;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.components.element.StateElement;

/**
 * @since 2021-05-12
 */
public class ColorListHelper {

    /**
     * 创建颜色选择器对象
     *
     * @param normalColor 未选择状态的颜色
     * @param selectedColor 选择状态的颜色
     * @return 选择器
     */
    public static StateElement makeStateElement(RgbColor normalColor, RgbColor selectedColor) {
        ShapeElement normalShapeElement = new ShapeElement();
        normalShapeElement.setRgbColor(normalColor);

        ShapeElement selectedShapeElement = new ShapeElement();
        selectedShapeElement.setRgbColor(selectedColor);

        StateElement stateElement = new StateElement();
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_EMPTY}, normalShapeElement);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_SELECTED}, selectedShapeElement);
        stateElement.addState(new int[]{ComponentState.COMPONENT_STATE_FOCUSED}, selectedShapeElement);

        return stateElement;
    }

    public static RgbColor getColorByState(StateElement stateElement, int[] stateArray) {
        int index = stateElement.findStateElementIndex(stateArray);
        if (index < 0) {
            throw new IllegalArgumentException("stateSet not found!");
        }

        ShapeElement currentElement = (ShapeElement) stateElement.getStateElement(index).getCurrentElement();
        return currentElement.getRgbColors()[0];
    }
}
